import sys

class Solution(object):
    def threeSumClosest(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        nums.sort()
        best = sys.maxsize
        for a in range(0, len(nums)-2):
            if a!=0 and nums[a]==nums[a-1]:continue

            left, right = a+1, len(nums)-1
            while left<right:
                s = nums[a]+nums[left]+nums[right]
                if abs(s-target)<abs(best-target): best = s
                if s < target:
                    left+=1
                elif s > target:
                    right-=1
                else:
                    break 
        return best

print(Solution().threeSumClosest([0,5,-1,-2,4,-1,0,-3,4,-5], 1))